package com.qire.manhua.model.po;

import android.util.Size;

import com.qire.manhua.dal.converter.ChapterContentConverter;

import java.util.List;

import androidx.room.Entity;
import androidx.room.Ignore;
import androidx.room.PrimaryKey;
import androidx.room.TypeConverters;

/**
 * 漫画章节-本地
 */
//@Entity(indices = {@Index(value = {"chapterId"}, unique = true)} ) // 建立索引，并设置成唯一键，可以防止重复写入，如果插入模式为 @Insert(onConflict = OnConflictStrategy.REPLACE) 则会使用最新记录的替换
@Entity
@TypeConverters({ChapterContentConverter.class}) // 转换器。用于转换复杂类型。这个与多表联查不一样，是将复杂类型转换成一个字段保存。
public class LocalComicChapter {

    @Ignore
    public static final int STATE_RUNNING = 0;
    @Ignore
    public static final int STATE_PAUSE = 1;
    @Ignore
    public static final int STATE_COMPLETE = 2;

    /** 章节ID，主键，选择了非自动增长，于服务器数据的章节ID同步 */
//    @PrimaryKey(autoGenerate = true)//主键是否自动增长，默认为false
    @PrimaryKey
    private int id;

    /** 章节序号，在章节目录里的排序，这里不一定指第几章因为存在序言或其他情况 */
    private int serialNumber;

    /** 所属漫画ID */
    private int comicId;

    /** 漫画封面 */
    private String comicCover;

    /** 漫画作者 */
    private String comicAuthor;

    /** 漫画名称 */
    private String comicName;

    /** 章节名称 */
    private String chapterName;

    /** 章节内容图片列表 */
    private List<String> contentList;

    /** 章节内容图片对应大小列表 */
    private List<Size> sizeList;

    /** 喜欢点赞人数 */
    private int likeCount;

    /** 已下载章节内容图片计数 */
    private int downloadCount;

    /** 当前下载状态：是否下载完成 */
    private int downloadState;


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getSerialNumber() {
        return serialNumber;
    }

    public void setSerialNumber(int serialNumber) {
        this.serialNumber = serialNumber;
    }

    public int getComicId() {
        return comicId;
    }

    public void setComicId(int comicId) {
        this.comicId = comicId;
    }

    public String getComicCover() {
        return comicCover;
    }

    public void setComicCover(String comicCover) {
        this.comicCover = comicCover;
    }

    public String getComicAuthor() {
        return comicAuthor;
    }

    public void setComicAuthor(String comicAuthor) {
        this.comicAuthor = comicAuthor;
    }

    public String getComicName() {
        return comicName;
    }

    public void setComicName(String comicName) {
        this.comicName = comicName;
    }

    public String getChapterName() {
        return chapterName;
    }

    public void setChapterName(String chapterName) {
        this.chapterName = chapterName;
    }

    public List<String> getContentList() {
        return contentList;
    }

    public void setContentList(List<String> contentList) {
        this.contentList = contentList;
    }

    public List<Size> getSizeList() {
        return sizeList;
    }

    public void setSizeList(List<Size> sizeList) {
        this.sizeList = sizeList;
    }

    public int getLikeCount() {
        return likeCount;
    }

    public void setLikeCount(int likeCount) {
        this.likeCount = likeCount;
    }

    public int getDownloadCount() {
        return downloadCount;
    }

    public void setDownloadCount(int downloadCount) {
        this.downloadCount = downloadCount;
    }

    public int getDownloadState() {
        return downloadState;
    }

    public void setDownloadState(int downloadState) {
        this.downloadState = downloadState;
    }

    public String currDownloadStateName() {
        switch (downloadState) {
            case STATE_RUNNING:
                return "下载中";
            case STATE_COMPLETE:
                return "已完成";
            case STATE_PAUSE:
            default:
                return "已暂停";
        }
    }

    /**
     * 是否下载完成
     * @return 完成 true 否则 false
     */
    public boolean isDownloadComplete() {
        return downloadState == STATE_COMPLETE;
    }

    /**
     * 是否正在下载中
     * @return 正在下载 true 否则 false
     */
    public boolean isDownloading() {
        return downloadState == STATE_RUNNING;
    }

    /**
     * 获得当前状态切换后的下一个状态，如果已完成则永远返回完成状态，如果是暂停则返回运行，反之亦然
     * @return {@code downloadState}
     */
    public int nextDownloadState() {
        switch (downloadState) {
            case STATE_COMPLETE :
                return STATE_COMPLETE;
            case STATE_PAUSE:
                return STATE_RUNNING;
            default:
                return STATE_PAUSE;
        }
    }

    @Override
    public String toString() {
        return "LocalComicChapter{" +
                "id=" + id +
                ", serialNumber=" + serialNumber +
                ", comicId=" + comicId +
                ", comicCover='" + comicCover + '\'' +
                ", comicAuthor='" + comicAuthor + '\'' +
                ", comicName='" + comicName + '\'' +
                ", chapterName='" + chapterName + '\'' +
                ", contentList=" + (contentList == null ? 0 :contentList.size()) +
                ", sizeList=" + (sizeList == null ? 0 : sizeList.size()) +
                ", likeCount=" + likeCount +
                '}';
    }

}
